package controllers.caterinaController.controllers;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import model.Model;


import person.Customer;
import person.Employee;
import budget.Budget;
import budget.Request;
import controllers.Checks;
import controllers.ExceptionsTypes;
import controllers.Permission;
import controllers.PermissionList;
import controllers.dbWrapper.BudgetWrapper;

public class BudgetManager {
	
	public static ArrayList<Budget> getAllBudget() throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		return BudgetWrapper.getAllBudget(true);
	}
	
	public static ArrayList<Budget> getBudget(Customer customer) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		ArrayList<Request> requests = RequestManager.getRequest(customer);
		ArrayList<Budget> budgets = new ArrayList<Budget>();
		for(Request r : requests){
			try{
				budgets.add(BudgetWrapper.getBudgetByRequest(r, true));
			}catch(Exception e){
				if(!e.getMessage().equalsIgnoreCase(ExceptionsTypes.NOT_FOUND.name()))
					throw e;
			}
		}
		return budgets;
	}
	
	public static ArrayList<Budget> getBudgetBySearchFilter(String filter) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		ArrayList<Budget> all = BudgetWrapper.getAllBudget(false);
		ArrayList<Budget> filtered = new ArrayList<Budget>();
		for(Budget b : all)
		if(b.contains(filter))
    		filtered.add(b);
		return filtered;
	}
	
	public static boolean existBudget(Request request) throws Exception{
		try{
			BudgetWrapper.getBudgetByRequest(request, false);
			return true;
		} catch(Exception e){
			if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.NOT_FOUND.name()))
				return false;
			else
				throw e;
		}
	}
	
	public static Budget generateBudget(Request request) throws Exception{
		PermissionList per = LoginManager.getPermission();
		if(per.can(Permission.REQUESTS_MANAGE)){
			if(BudgetManager.existBudget(request)) //per verificare che non esista gi�
				throw new Exception(ExceptionsTypes.EXIST_ERROR.name());
			try{
				RequestManager.getRequest(request.getId()); //per verificare che sia salvata nel db
			}catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.NOT_FOUND.name()))
					throw new Exception(ExceptionsTypes.DIPENDENCE_ERROR.name());
				throw e;
			}
			
			GregorianCalendar lastDate = new GregorianCalendar();
			lastDate.add(GregorianCalendar.MONTH, EXPIRATION_TIME_IN_MONTH);
			Budget b = new Budget(Model.NO_ID, request);
			b.setLastDate(lastDate);
			b.calculateTotalCostWhitoutAdjustment();
			
			Checks.checkBudget(b);
			BudgetWrapper.insertBudget(true, b);
			
			MailManager.sendRequestAccepted((Employee) LoginManager.getCurrentLogged(), request);
			return b;}
		else
			throw new Exception(ExceptionsTypes.PERMISSION_DENIDED.name());
	}
	
	public static void modifyBudget(Budget budget) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		budget.calculateTotalCostWhitoutAdjustment();
		Checks.checkBudget(budget);
		BudgetWrapper.modifyBudget(true, budget);
	}
	
	public static void removeBudget(Budget budget) throws Exception{
		LoginManager.getCurrentLogged(); //per verificare lo stato del login
		BudgetWrapper.deleteBudget(true, budget);
	}
	
	private static final int EXPIRATION_TIME_IN_MONTH = 6;
}
